import { BasicColumn, FormSchema } from '@/components/table';
import { rules } from '@pkg/utils';
import { getBusCompanyList, getOrganizationList, isExistByUsername } from './user.api';
import { RenderType } from '@pkg/setting';

/**
 * 表格列定义
 */
export const columns: BasicColumn[] = [
  /*  {
    title: '头像',
    dataIndex: 'avatar',
    width: 120,
    isExport: false,
    customRender: render.renderAvatar,
  },*/
  {
    title: '真实名称',
    dataIndex: 'nickName',
    width: 120,
  },
  {
    title: '用户名',
    dataIndex: 'userName',
    width: 120,
  },
  {
    title: '角色',
    dataIndex: 'roleNames',
    width: 150,
  },
  {
    title: '手机号',
    dataIndex: 'mobile',
    width: 120,
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 80,
    dictData: [
      { label: '禁用', value: 0, color: '#a6a9a6' },
      { label: '启用', value: 1, color: '#0930f1' },
    ],
    renderType: RenderType.DICT,
  },
  {
    title: '最后登录时间',
    dataIndex: 'lastLoginTime',
    width: 150,
  },
  {
    title: '合作企业',
    dataIndex: 'companyNames',
    width: 150,
  },
  {
    title: '创建日期',
    dataIndex: 'createTime',
    width: 150,
  },
  {
    title: '审核状态',
    dataIndex: 'checkState',
    width: 100,
    dictData: [
      { label: '待审核', value: 0, color: '#a6a9a6' },
      { label: '审核通过', value: 1, color: '#09d550' },
      { label: '审核驳回', value: 2, color: '#6b340f' },
      { label: '审核拒绝', value: 3, color: '#f93c42' },
    ],
    renderType: RenderType.DICT,
  },
  {
    title: '导出天数',
    dataIndex: 'exportDays',
    width: 100,
    customRender: ({ record }: Recordable) => {
      if (record.exportDays == 0) {
        return '无限制';
      }
      return record.exportDays;
    },
  },
  {
    title: '创建人',
    dataIndex: 'submitUserName',
    width: 150,
  },
  {
    title: '创建用途',
    dataIndex: 'userDesc',
    width: 150,
  },
  {
    title: '审核备注',
    dataIndex: 'checkDesc',
    width: 250,
  },
  /*{
    title: '状态',
    dataIndex: 'status',
    width: 120,
    customRender: ({ record, value }: Recordable) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.status === 1,
        checkedChildren: '已启用',
        unCheckedChildren: '已禁用',
        loading: record.pendingStatus,
        onChange() {
          record.pendingStatus = false;
          setStatus(record.userId)
            .then((data) => {
              if (value == 0) {
                record.status = 1;
              }
              if (value == 1) {
                record.status = 0;
              }
            })
            .catch((data) => {
              console.info('catch', data);
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '邮箱',
    dataIndex: 'email',
    width: 100,
  },
  {
    title: '性别',
    dataIndex: 'sex',
    width: 100,
    customRender: render.renderDict,
  },*/
];

/**
 * 列表查询定义
 */
export const searchFormSchema: FormSchema[] = [
  {
    label: '用户名',
    field: 'userName',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '真实名称',
    field: 'nickName',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '角色名称',
    field: 'roleName',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '手机号码',
    field: 'mobile',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '合作企业',
    field: 'busCompanyId',
    component: 'ApiSelect',
    componentProps: {
      api: getBusCompanyList,
      labelField: 'companyName',
      valueField: 'companyId',
    },
  },
  {
    field: 'status',
    label: '状态',
    component: 'Select',
    colProps: { span: 5 },
    componentProps: {
      options: [
        { label: '已启用', value: 1 },
        { label: '已禁用', value: 0 },
      ],
    },
  },
  {
    field: 'checkState',
    label: '审核状态',
    component: 'Select',
    colProps: { span: 5 },
    componentProps: {
      options: [
        { label: '待审核', value: 0 },
        { label: '审核通过', value: 1 },
        { label: '审核驳回', value: 2 },
        { label: '审核拒绝', value: 3 },
      ],
    },
  },
];

/**
 * 表单定义
 */
export const formSchema: FormSchema[] = [
  {
    label: '用户Id',
    field: 'userId',
    component: 'Input',
    show: false,
  },
  {
    label: '审核状态',
    field: 'checkState',
    component: 'Input',
    show: false,
  },
  {
    label: '所属机构',
    field: 'organizationId',
    component: 'ApiSelect',
    required: true,
    componentProps: {
      api: getOrganizationList,
      labelField: 'organizationName',
      valueField: 'organizationId',
    },
  },
  {
    label: '账户名',
    field: 'userName',
    required: true,
    component: 'Input',
    dynamicDisabled: ({ values }) => {
      if (values.userId) {
        return true;
      } else {
        return false;
      }
    },
    dynamicRules: ({ model }) => {
      return [
        {
          required: true,
          validator: async (_, value) => {
            if (model.userId) {
              return Promise.resolve();
            }
            if (!value) {
              return Promise.reject('请输入账号名');
            }
            let isExist = await isExistByUsername(value);
            if (isExist) {
              return Promise.reject('该账号名已存在');
            }
            return Promise.resolve();
          },
          trigger: 'change',
        },
      ];
    },
  },
  {
    label: '真实名称',
    field: 'nickName',
    component: 'Input',
  },
  {
    label: '密码',
    field: 'password',
    component: 'Input',
    ifShow: ({ values }) => !values.userId,
    dynamicRules: ({ values }) => rules.checkPassword(values, true),
    componentProps: {
      placeholder: '密码长度大于6位，且包含大小写字母或包含特殊字符',
    },
  },
  {
    label: '确认密码',
    field: 'confirmPassword',
    component: 'InputPassword',
    dynamicRules: ({ values }) => rules.confirmPassword(values, true),
    ifShow: ({ values }) => !values.userId,
  },
  {
    label: '手机号',
    field: 'mobile',
    component: 'Input',
  },
  {
    label: '邮箱',
    field: 'email',
    component: 'Input',
  },
  {
    label: '导出设置(天)',
    field: 'exportDays',
    component: 'InputNumber',
    required: true,
    componentProps: {
      style: { width: '350px' },
      min: 0,
      max: 1200,
      addonAfter: '0代表不限制导出天数',
    },
    dynamicRules: () => {
      return [
        {
          required: true,
          validator: async (_, value) => {
            if (!value && value != 0) {
              return Promise.reject('请输入数字!');
            }
            if (!new RegExp(/^\d+$/).test(value)) {
              return Promise.reject('数字格式有误');
            }
            return Promise.resolve();
          },
          trigger: 'change',
        },
      ];
    },
  },
  {
    label: '创建用途',
    field: 'userDesc',
    component: 'InputTextArea',
  },
];

/**
 * 表单定义
 */
export const passwordFormSchema: FormSchema[] = [
  {
    label: '用户Id',
    field: 'userId',
    component: 'Input',
    show: false,
  },
  {
    label: '密码',
    field: 'password',
    component: 'Input',
    dynamicRules: ({ values }) => rules.checkPassword(values, true),
    componentProps: {
      placeholder: '密码长度大于6位，且包含大小写字母或包含特殊字符',
    },
  },
  {
    label: '确认密码',
    field: 'confirmPassword',
    component: 'InputPassword',
    dynamicRules: ({ values }) => rules.confirmPassword(values, true),
  },
];

export const checkFormSchema: FormSchema[] = [
  {
    label: '用户Id',
    field: 'userId',
    component: 'Input',
    required: false,
    show: false,
  },
  {
    label: '账户',
    field: 'userName',
    component: 'Input',
    required: false,
    dynamicDisabled: true,
  },
  {
    label: '姓名',
    field: 'nickName',
    component: 'Input',
    required: false,
    dynamicDisabled: true,
  },
  {
    label: '账号用途',
    field: 'userDesc',
    component: 'InputTextArea',
    required: false,
    dynamicDisabled: true,
  },
  {
    label: '审核状态',
    field: 'checkState',
    component: 'RadioGroup',
    required: false,
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '待审核', value: 0 },
        { label: '审核通过', value: 1 },
        { label: '审核驳回', value: 2 },
        { label: '审核拒绝', value: 3 },
      ],
    },
  },
  {
    label: '审核备注',
    field: 'checkDesc',
    component: 'InputTextArea',
    required: false,
  },
];

export const waitCheckFormSchema: FormSchema[] = [
  {
    label: '用户Id',
    field: 'userId',
    component: 'Input',
    required: false,
    show: false,
  },
  {
    label: '账户',
    field: 'userName',
    component: 'Input',
    required: false,
    dynamicDisabled: true,
  },
  {
    label: '姓名',
    field: 'nickName',
    component: 'Input',
    required: false,
    dynamicDisabled: true,
  },
  {
    label: '驳回原因',
    field: 'checkDesc',
    component: 'InputTextArea',
    required: false,
    dynamicDisabled: true,
  },
  {
    label: '账号用途',
    field: 'userDesc',
    component: 'InputTextArea',
    required: true,
  },
];
